package com.googlecode.fxutils;

/*
Copyright (c) 2014, Carlos Eduardo Klock.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.stage.Popup;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

/**
 * Utilities to display popup windows.
 */
public final class PopupUtils {
    
    private PopupUtils() {
    }
    
    public static Popup createPopup(final Parent content, final PopupProperties properties) {
        if (content == null) {
            throw new NullPointerException();
        }
        if (properties == null) {
            throw new NullPointerException();
        }
        
        final Window owner = properties.getOwnerWindow();
        final Popup p = new Popup();
        
        p.setAutoFix(properties.isAutoFix());
        p.setAutoHide(properties.isAutoHide());
        p.setHideOnEscape(properties.isHideOnEscape());
        if (properties.getInlineStyle() != null) {
            content.setStyle(properties.getInlineStyle());
        }
        
        p.getContent().setAll(content);
                
        p.setOnShown(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent e) {
                if (properties.isCentralizeOnOwner()) {
                    // Centralize on owner window after popup size is defined.
                    centralizeOnOwner(p, owner);
                } else if (properties.getX() != 0 && properties.getY() != 0) {
                    p.setX(properties.getX() - p.getWidth());
                    p.setY(properties.getY() - p.getHeight());
                }
            }
        });
        
        return p;
    }
    
    public static Popup showPopupMsg(final String msg, final PopupProperties properties) {
        final Label label = new Label(msg);
        final Popup p = createPopup(label, properties);
        label.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent e) {
                p.hide();
            }
        });        
        p.show(properties.getOwnerWindow());
        return p;
    }
    
    public static Popup showPopupMsg(final String msg, final Window owner) {
        return showPopupMsg(msg, PopupProperties.createMessageStyleProperties(owner, true));
    }
    
    public static void centralizeOnOwner(final Popup p, final Window owner) {
        p.setX(owner.getX() + owner.getWidth()/2 - p.getWidth()/2);
        p.setY(owner.getY() + owner.getHeight()/2 - p.getHeight()/2);
    }
        
}
